package com.jiliang.auth.controller;


import cn.hutool.core.collection.CollectionUtil;
import com.jiliang.auth.controller.req.AuthUserFuncReqForm;
import com.jiliang.auth.controller.res.AuthFuncResForm;
import com.jiliang.auth.controller.res.AuthUserFuncResForm;
import com.jiliang.auth.dao.model.AuthUserFunc;
import com.jiliang.auth.domain.AuthFuncBo;
import com.jiliang.auth.domain.AuthUserFuncBo;
import com.jiliang.auth.service.AuthUserFuncService;
import com.jiliang.common.bean.BeanCopierDsUtil;
import com.jiliang.common.bean.BeanCopyDsUtil;
import com.jiliang.common.constant.CommonDsConstant;
import com.jiliang.common.result.WebResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户功能管理controller
 *
 * @author 
 * @version 1.0
 * @Date  17:59
 */
@RestController
@RequestMapping(value = "/userFunc")
//@Api("AuthUserFuncController相关的api")
public class AuthUserFuncController {

    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Resource
    private AuthUserFuncService authUserFuncService;

//    @ApiOperation(value = "条件查询授权人boId", notes = "查询参数")
//    @ApiImplicitParam(name = "AuthUserFuncReqForm ", value = "查询参数", required = true, dataType = "AuthUserFuncReqForm ")
    @RequestMapping(value = "/selAuthBoIdListByParam", method = RequestMethod.POST)
    public WebResult selAuthBoIdListByParam(@RequestBody AuthUserFuncReqForm reqForm, BindingResult result) {
//        ApiResult webAuthResult = ApiAuthVerification.getResult(reqForm, result);
//        if (!webAuthResult.isSuccess) {
//            return webAuthResult.getWebResult();
//        }
        AuthUserFuncBo userFuncBo = new AuthUserFuncBo();
        BeanCopyDsUtil.copyProperties(reqForm, userFuncBo);
        List<String> userFuncIdList = authUserFuncService.selAuthBoIdListByParam(userFuncBo);
        return WebResult.success(userFuncIdList);
    }

//    @ApiOperation(value = "查询用户功能是否可用", notes = "查询参数")
//    @ApiImplicitParam(name = "AuthUserFuncReqForm ", value = "查询参数", required = true, dataType = "AuthUserFuncReqForm ")
    @RequestMapping(value = "/selCanUse", method = RequestMethod.POST)
    public WebResult selCanUse( @RequestBody AuthUserFuncReqForm reqForm, BindingResult result) {
//        ApiResult webAuthResult = ApiAuthVerification.getResult(reqForm, result);
//        if (!webAuthResult.isSuccess) {
//            return webAuthResult.getWebResult();
//        }
        AuthUserFuncBo userFuncBo = new AuthUserFuncBo();
        BeanCopyDsUtil.copyProperties(reqForm, userFuncBo);
        List<AuthUserFunc> userFuncBoList = authUserFuncService.selUserFuncListByParam(userFuncBo);
        if (CollectionUtil.isEmpty(userFuncBoList)) {
            return WebResult.success(CommonDsConstant.DEFAULT_INT_NO);
        }
        return WebResult.success(CommonDsConstant.DEFAULT_INT_YES);
    }

//    @ApiOperation(value = "查询用户功能", notes = "查询参数")
//    @ApiImplicitParam(name = "AuthUserFuncReqForm ", value = "查询参数", required = true, dataType = "AuthUserFuncReqForm ")
    @RequestMapping(value = "/selUserFuncListByParam", method = RequestMethod.POST)
    public WebResult selUserFuncListByParam( @RequestBody AuthUserFuncReqForm reqForm, BindingResult result) {
//        ApiResult webAuthResult = ApiAuthVerification.getResult(reqForm, result);
//        if (!webAuthResult.isSuccess) {
//            return webAuthResult.getWebResult();
//        }
        AuthUserFuncBo userFuncBo = new AuthUserFuncBo();
        BeanCopyDsUtil.copyProperties(reqForm, userFuncBo);
        List<AuthUserFuncBo> userFuncBoList = authUserFuncService.selUserFuncWithFuncListByParam(userFuncBo);
        List<AuthUserFuncResForm> resFormList = modelToResList(userFuncBoList);
        return WebResult.success(resFormList);
    }

//    @ApiOperation(value = "设置用户功能", notes = "添加参数")
//    @ApiImplicitParam(name = "AuthUserFuncReqForm ", value = "添加参数", required = true, dataType = "AuthUserFuncReqForm ")
    @RequestMapping(value = "/addUserFuncs", method = RequestMethod.POST)
    public WebResult addUserFuncs( @RequestBody AuthUserFuncReqForm userFuncReqForm, BindingResult result) {
//        ApiResult webAuthResult = ApiAuthVerification.getResult(userFuncReqForm, result);
//        if (!webAuthResult.isSuccess) {
//            return webAuthResult.getWebResult();
//        }
        AuthUserFuncBo userFuncBo = new AuthUserFuncBo();
        BeanCopyDsUtil.copyProperties(userFuncReqForm, userFuncBo);
        authUserFuncService.addUserFuncs(userFuncBo);
        return WebResult.success();
    }

    /**
     * 查询用户是否授权当前功能（若非末级节点则连带查询子孙节点）
     *
     * @param reqForm
     * @return com.nxin.etpojar.common.result.WebResult
     * @author 
     * @date 13:12 
     */
//    @ApiOperation(value = "设置用户功能", notes = "添加参数")
//    @ApiImplicitParam(name = "AuthUserFuncReqForm ", value = "添加参数", required = true, dataType = "AuthUserFuncReqForm ")
    @RequestMapping(value = "/selSpecUserFunc", method = RequestMethod.POST)
    public WebResult selSpecUserFunc( @RequestBody AuthUserFuncReqForm reqForm, BindingResult result) {
//        ApiResult webAuthResult = ApiAuthVerification.getResult(reqForm, result);
//        if (!webAuthResult.isSuccess) {
//            return webAuthResult.getWebResult();
//        }
        AuthUserFuncBo userFuncBo = BeanCopierDsUtil.copyProperties(reqForm, AuthUserFuncBo.class);
        AuthFuncBo authFuncBo = authUserFuncService.selSpecUserFunc(userFuncBo);
        if (authFuncBo == null) {
            return WebResult.success();
        }
        AuthFuncResForm resForm = BeanCopierDsUtil.copyProperties(authFuncBo, AuthFuncResForm.class);
        return WebResult.success(resForm);
    }

    private List<AuthUserFuncResForm> modelToResList(List<AuthUserFuncBo> userFuncList) {
        if (CollectionUtil.isEmpty(userFuncList)) {
            return null;
        }
        List<AuthUserFuncResForm> resFormList = new ArrayList<AuthUserFuncResForm>();
        AuthUserFuncResForm resForm = null;
        for (AuthUserFuncBo userFunc : userFuncList) {
            resForm = modelToRes(userFunc);
            resFormList.add(resForm);
        }
        return resFormList;
    }

    private AuthUserFuncResForm modelToRes(AuthUserFuncBo userFunc) {
        if (userFunc == null) {
            return null;
        }
        AuthUserFuncResForm resForm = new AuthUserFuncResForm();
        BeanCopyDsUtil.copyProperties(userFunc, resForm);
        return resForm;
    }
}
